﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SCRUM.Core.DAO;
using SCRUM.Core.BusinessObjects;

/// <summary>
/// Widget showing users in specified project/team and role.
/// </summary>
public partial class UserOverview : DataOverviewWidget<User> //WidgetBase
{
    #region [ USEROVERVIEW ]

    private IList<User> users;

    /// <summary>
    /// Number of columns (from left) which are always displayed
    /// </summary>
    private int userColumnsOffset = 1;

    #endregion

    /// <summary>
    /// Gets the name of the widget. It must be exactly the same as the folder that contains the widget.
    /// </summary>
    /// <value></value>
    public override string Name
    {
        get { return "UserOverview"; }
    }

    /// <summary>
    /// Gets the title. Title is shown in the header of the widget container.
    /// </summary>
    /// <value>The title.</value>
    public override string Title
    {
        get { return (string)GetLocalResourceObject("Title"); } 
    }

    #region [UserOverviewWidget Settings]

    /// <summary>
    /// Gets a value indicating whether this instance is editable.
    /// </summary>
    /// <value>
    /// 	<c>true</c> if this instance is editable; otherwise, <c>false</c>.
    /// </value>
    public override bool IsEditable
    {
        get { return true; }
    }

    /// <summary>
    /// Shows the edit.
    /// </summary>
    public override void ShowEdit()
    {
        base.ShowEdit();
        this.EditUserOverviewWidget.Visible = true;
    }

    /// <summary>
    /// Hides the edit.
    /// </summary>
    public override void HideEdit()
    {
        base.HideEdit();
        this.EditUserOverviewWidget.Visible = false;
    }

    /// <summary>
    /// Occurs when user clicks on Save Settings of PrototypeWidget
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void SaveSettingsClick(object sender, EventArgs e)
    {
        SaveSettings();
        this.HideEdit();
        LoadWidget();
    }

    /// <summary>
    /// Refreshes settings section with data from private member settings
    /// </summary>
    private void ShowSettings()
    {
        LabelPageSize.Text = settings["pageSize"];
    }

    #endregion

    /// <summary>
    /// Sets handlers for events.
    /// </summary>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    protected override void OnInit(EventArgs e)
    {
        GridViewData.PageIndexChanging += new GridViewPageEventHandler(GridViewData_SelectedPageChanged);
    }

    /// <summary>
    /// This method works as a substitute for Page_Load. You should use this method for
    /// data binding etc. instead of Page_Load.
    /// </summary>
    public override void LoadWidget()
    {
        LoadAvailableColumns();
        ApplySettings();
        if (!IsPostBack)
        {
            RefreshWidget();
        }
    }

    /// <summary>
    /// Applies the filter.
    /// </summary>
    /// <param name="sender">The sender.</param>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    protected void ApplyFilter(object sender, EventArgs e)
    {
        RefreshWidget();
    }

    /// <summary>
    /// Clears the filter.
    /// </summary>
    /// <param name="sender">The sender.</param>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    protected void ClearFilter(object sender, EventArgs e)
    {
        ClearTextBoxes();
        RefreshWidget();
    }

    private void ClearTextBoxes()
    {
        Login.Text = string.Empty;
        Fullname.Text = string.Empty;
        Email.Text = string.Empty;
        Telephone.Text = string.Empty;
        IM.Text = string.Empty;
        RoleSelection.ResetRole();
    }

    /// <summary>
    /// Called when [row data bound].
    /// </summary>
    /// <param name="sender">The sender.</param>
    /// <param name="e">The <see cref="System.Web.UI.WebControls.GridViewRowEventArgs"/> instance containing the event data.</param>
    protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
    {
        User user = e.Row.DataItem as User;
        if (user != null)
        {
            HyperLink emailHyperlink = (HyperLink)e.Row.FindControl("HyperlinkEmail");
            emailHyperlink.NavigateUrl = String.Format("mailto:{0}", user.Email);
            emailHyperlink.Text = user.Email;
        }
    }

    
    public override void RefreshWidget()
    {
        base.RefreshWidget();
        bool itemSelected = this.data.SelectedIndex != -1;
    }    

    protected override IList<User> itemsAcordingToFilter
    {
        get 
        {
            string login = Login.Text != string.Empty ? Login.Text : null;
            string fullname = Fullname.Text != string.Empty ? Fullname.Text : null;
            string email = Email.Text != string.Empty ? Email.Text : null;
            string telephone = Telephone.Text != string.Empty ? Telephone.Text : null;
            string im = IM.Text != string.Empty ? IM.Text : null;
            int roleID = RoleSelection.SelectedRoleID;
            return DAOUser.GetUsers(-1, login, fullname, email, true, telephone, im, roleID);
        }
    }

    protected override int alwaysVisibleColumn
    { get { return 0; }
    }

    protected override GridView data
    {
        get { return this.GridViewData; }
    }

    protected override Panel availableColumnsPanel
    {
        get { return this.AvailableColumns; }
    }

    protected override TextBox textBoxPageSize
    {
        get { return this.TextBoxPageSize; }
    }

}
